package com.example.algorithm.dualpointer;

/**
 * 盛最多水的容器
 * Leetcode-11
 */
public class ContainWater {

    public int maxArea(int[] height) {
        // 双指针，一左一右往中间走
        int l = 0, r = height.length - 1;
        int s = 0;
        while (l < r) {
            // 短柱移动面积才有可能增大
            int minH;
            if (height[l] < height[r]) {
                minH = height[l];
                s = Math.max(s, (r - l) * height[l]);
            } else {
                minH = height[r];
                s = Math.max(s, (r - l) * height[r]);
            }
            while (height[l] <= minH && l < r) {
                l++;
            }
            while (height[r] <= minH && l < r) {
                r--;
            }
        }
        return s;
    }
}
